package com.wnxy.admin.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.github.pagehelper.PageInfo;
import com.wnxy.admin.common.ResponseResult;
import com.wnxy.admin.dto.*;
import com.wnxy.admin.service.IChargingReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 能耗报表 前端控制器
 * </p>
 *
 * @author nhj
 */

@Api(tags = "（分析报表）接口")
@RestController
@RequestMapping("/ChargingPileReport")
public class ChargingPileReportController {

    private final IChargingReportService chargingReportServiceImpl;


    public ChargingPileReportController(IChargingReportService chargingReportServiceImpl) {
        this.chargingReportServiceImpl = chargingReportServiceImpl;
    }


    @SneakyThrows
    @ApiOperation(value = "导出运营分析数据")
    @GetMapping("getOperatorExport")
    public void getOperatorExport(HttpServletResponse response) {


        setExcelRespProp(response, "运营分析数据");//原始文件名
        List<OperatorReportDto> operatorExport = chargingReportServiceImpl.getOperatorExport();
        EasyExcel.write(response.getOutputStream())//获取到resp的输出流
                .head(OperatorReportDto.class)   //数据类型字节码
                .excelType(ExcelTypeEnum.XLSX)//指定excel的类型
                .sheet("运营分析数据信息")//工作薄名称
                .doWrite(operatorExport);//写入数据

    }


    private void setExcelRespProp(HttpServletResponse response, String rawFileName) throws UnsupportedEncodingException {

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

        String fileName = URLEncoder.encode(rawFileName, "UTF-8")
                .replaceAll("\\+", "%20");
        response.setHeader("Content-disposition",
                "attachment;filename*=utf-8''" + fileName + ".xlsx");
    }


    @ApiOperation(value = "运营分析")
    @GetMapping("getOperatorReport")
    public ResponseResult getOperatorReport(Integer pageNum, Integer pageSize,Long chargingStationId,String createTime) {

        PageInfo<OperatorReportDto> operatorReport = chargingReportServiceImpl.getOperatorReport(pageNum, pageSize,chargingStationId,createTime);

        return ResponseResult.ok(operatorReport);
    }

    @ApiOperation(value = "客户分析")
    @GetMapping("customerReport")
    public ResponseResult customerReport(Integer pageNum, Integer pageSize,Long chargingStationId,String createTime) {

        PageResult<CustomerReportDto> customerReportPageInfo =
                chargingReportServiceImpl.customerReport(pageNum,pageSize,chargingStationId,createTime);

        return ResponseResult.ok(customerReportPageInfo);
    }
    @SneakyThrows
    @ApiOperation(value = "导出客户分析数据")
    @GetMapping("getCustomerExport")
    public void getCustomerExport(HttpServletResponse response) {


        setExcelRespProp(response, "客户分析数据");//原始文件名

        List<CustomerReportDto> customerExport = chargingReportServiceImpl.getCustomerExport(null, null);
        EasyExcel.write(response.getOutputStream())//获取到resp的输出流
                .head(CustomerReportDto.class)   //数据类型字节码
                .excelType(ExcelTypeEnum.XLSX)//指定excel的类型
                .sheet("客户分析数据")//工作薄名称
                .doWrite(customerExport);//写入数据

    }


    @ApiOperation(value = "财务报表")
    @GetMapping("financeReport")
    public ResponseResult financeReport(Integer pageNum, Integer pageSize,String customerName) {

        PageResult<FinanceReportDto> financeReportDtoPageResult = chargingReportServiceImpl.financeReport(pageNum, pageSize, customerName);

        return ResponseResult.ok(financeReportDtoPageResult);
    }

    @SneakyThrows
    @ApiOperation(value = "导出财务报表数据")
    @GetMapping("getFinanceExport")
    public void getFinanceExport(HttpServletResponse response) {


        setExcelRespProp(response, "财务报表数据");//原始文件名

        List<FinanceReportDto> financeExport = chargingReportServiceImpl.getFinanceExport();

        EasyExcel.write(response.getOutputStream())//获取到resp的输出流
                .head(FinanceReportDto.class)   //数据类型字节码
                .excelType(ExcelTypeEnum.XLSX)//指定excel的类型
                .sheet("财务报表数据")//工作薄名称
                .doWrite(financeExport);//写入数据

    }


    @ApiOperation(value = "能耗报表")
    @GetMapping("nengHaoReport")
    public ResponseResult nengHaoReport(Integer pageNum, Integer pageSize, Long id, String endTime) {

        PageResult<ChargingPileReportDto> item = chargingReportServiceImpl.nengHaoReport(pageNum,pageSize,id,endTime);

        return ResponseResult.ok(item);
    }

}
